﻿using System;
using static SysHook.SysImport.User32;
using static SysHook.SysImport.Kernel32;

namespace SysHook
{
    public class KeyboardHook
    {
        IntPtr hhook { get; set; } = IntPtr.Zero;
        private KeyboardHook()
        {
            hook();
        }
        private void hook()
        {
            IntPtr hInstance = LoadLibrary("User32");
            hhook = SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, hInstance, 0);
        }
        private void unhook()
        {
            if (hhook != IntPtr.Zero)
            {
                unhook();
                hhook = IntPtr.Zero;
            }
        }
        ~KeyboardHook()
        {
            unhook();
        }
        internal int hookProc(int code, int wParam, ref keyboardHookStruct lParam)
        {
            if (code >= 0)
            {
                //Keys key = (Keys)lParam.vkCode;
                //if (HookedKeys.Contains(key))
                //{
                //    KeyEventArgs kea = new KeyEventArgs(key);
                //    if ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) && (KeyDown != null))
                //    {
                //        KeyDown(this, kea);
                //    }
                //    else if ((wParam == WM_KEYUP || wParam == WM_SYSKEYUP) && (KeyUp != null))
                //    {
                //        KeyUp(this, kea);
                //    }
                //    if (kea.Handled)
                //        return 1;
                //}
            }
            return CallNextHookEx(hhook, code, wParam, ref lParam);
        }
    }
}
